//
//  MineViewController.m
//  网络FM
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//
#import "MineViewController.h"
#import "MineTableViewCell.h"
#import "LikeViewController.h"
#import "HistoryViewController.h"
#import "SettingViewController.h"
#import "WeiboSDK.h"
#import "CellectDatabase.h"
#import "HistoryDatabase.h"
#import <UIImageView+WebCache.h>


#define kAppKey         @"2958669015"
#define kRedirectURI    @"http://www.sina.com"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MineViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic ,retain) UIImageView *backImage;

@property (nonatomic, retain) UIImageView *userImage;
@property (nonatomic, retain) UILabel *userNameLabel;

@end

@implementation MineViewController

- (void)dealloc
{
    [_userImage release];
    [_userNameLabel release];
    [_backImage release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
        
    }
    return self;
}

- (void)getUserInformation:(NSNotification *)notification
{
    NSDictionary *dic = [notification object];
//    NSLog(@"dic = %@", dic);
    
    [self.userImage sd_setImageWithURL:[dic objectForKey:@"avatar_hd"] placeholderImage:[UIImage imageNamed:@"头像.jpg"] options:SDWebImageRetryFailed];
    self.userNameLabel.text = [dic objectForKey:@"screen_name"];
    
}


- (void)viewDidLoad {
    
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self createView];
    
    // 注册消息中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserInformation:) name:@"userInformation" object:nil];
    
    //注册注销消息中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getInformation) name:@"logout" object:nil];

}

- (void)getInformation
{
    [self.userImage setImage:[UIImage imageNamed:@"头像.jpg"]];
    self.userNameLabel.text = @"未登录";
    
}


- (void)createView
{
    self.backImage = [[UIImageView alloc] initWithFrame:self.view.frame];
    self.backImage.userInteractionEnabled = YES;
    [_backImage setImage:[UIImage imageNamed:@"抽屉界面背景图.jpg"]];
    [self.view addSubview:_backImage];
    [_backImage release];
    
    self.userImage = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH / 3 * 2 - WIDTH / 3) / 2, HEIGHT / 7, WIDTH / 3, WIDTH / 3)];
    [_userImage setImage:[UIImage imageNamed:@"头像.jpg"]];
    _userImage.layer.cornerRadius = 10;
    _userImage.clipsToBounds = YES;
    [_userImage setUserInteractionEnabled:YES];
    // 给imageView添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userImageClick:)];
    [_userImage addGestureRecognizer:tap];
    [tap release];
    [_backImage addSubview:_userImage];
    [_userImage release];
    
    self.userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, HEIGHT / 6 + WIDTH / 3, WIDTH / 3 * 2, 20)];
    _userNameLabel.text = @"未登录";
    _userNameLabel.textColor = [UIColor whiteColor];
    _userNameLabel.font = [UIFont boldSystemFontOfSize:17];
    _userNameLabel.textAlignment = NSTextAlignmentCenter;
    [_backImage addSubview:_userNameLabel];
    [_userNameLabel release];
    
    
//    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
//    loginButton.titleLabel.font = [UIFont boldSystemFontOfSize:22];
//    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [loginButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    loginButton.frame = CGRectMake(10, self.userNameLabel.frame.origin.y + 40, WIDTH / 4 + 10, HEIGHT / 15);
//    [loginButton setBackgroundColor:[UIColor colorWithRed:210 / 255.0 green:210 / 255.0 blue:210 / 255.0 alpha:0.2f]];
//    loginButton.layer.cornerRadius = 5;
//    loginButton.clipsToBounds = YES;
//    [_backImage addSubview:loginButton];
//    [loginButton release];
//    
//    
//    
//    UIButton *logOutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [logOutButton setTitle:@"注销" forState:UIControlStateNormal];
//    [logOutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    logOutButton.titleLabel.font = [UIFont boldSystemFontOfSize:22];
//    [logOutButton addTarget:self action:@selector(logOutButtonClick) forControlEvents:UIControlEventTouchUpInside];
//    logOutButton.frame = CGRectMake(loginButton.frame.origin.x + WIDTH / 4 + 20, self.userNameLabel.frame.origin.y + 40, WIDTH / 4 + 10, HEIGHT / 15);
//    [logOutButton setBackgroundColor:[UIColor colorWithRed:210 / 255.0 green:210 / 255.0 blue:210 / 255.0 alpha:0.2f]];
//    logOutButton.layer.cornerRadius = 5;
//    logOutButton.clipsToBounds = YES;
//    [_backImage addSubview:logOutButton];
//    [logOutButton release];
    
    
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(10, self.userNameLabel.frame.origin.y + 30, [[UIScreen mainScreen] bounds].size.width / 3 * 2 - 20, HEIGHT / 3) style:UITableViewStylePlain];
    table.rowHeight = HEIGHT / 8;
    [table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [table setBackgroundColor:[UIColor clearColor]];
    table.scrollEnabled = NO;
    table.dataSource = self;
    table.delegate = self;
    [_backImage addSubview:table];
    [table release];
    
    UIButton *setButton = [[UIButton alloc] initWithFrame:CGRectMake(20, HEIGHT / 10 * 9, 80, 40)];
    [setButton setBackgroundColor:[UIColor clearColor]];
    [setButton addTarget:self action:@selector(setButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_backImage addSubview:setButton];
    [setButton release];
    
    UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 25, 25)];
    [aButton setImage:[UIImage imageNamed:@"设置.png"] forState:UIControlStateNormal];
    [aButton setImage:[UIImage imageNamed:@"设置.png"] forState:UIControlStateHighlighted];
    [aButton addTarget:self action:@selector(setButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [setButton addSubview:aButton];
    [aButton release];
    
    UIButton *bButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 5, 50, 30)];
    [bButton setTitle:@"设置" forState:UIControlStateNormal];
    bButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [bButton addTarget:self action:@selector(setButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [setButton addSubview:bButton];
    [bButton release];
    
}


// 新浪登录按钮点击事件
- (void)userImageClick:(id)sender
{
    
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = kRedirectURI;
    request.scope = @"all";
    request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    [WeiboSDK sendRequest:request];
    
}

//// 登出按钮点击事件
//- (void)logOutButtonClick
//{
//    MineViewController *myDelegate =(MineViewController*)[[UIApplication sharedApplication] delegate];
//    [WeiboSDK logOutWithToken:myDelegate.wbtoken delegate:self withTag:@"user1"];
//    
//}


//#pragma mark -
//#pragma WBHttpRequestDelegate
//
//- (void)request:(WBHttpRequest *)request didFinishLoadingWithResult:(NSString *)result
//{
//    UIAlertView *alert = nil;
//    
//    alert = [[UIAlertView alloc] initWithTitle:@"已注销!" message:[NSString stringWithFormat:@""] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//    [alert show];
//    [alert release];
//    
//    [self.userImage setImage:[UIImage imageNamed:@"头像.jpg"]];
//    self.userNameLabel.text = @"用户名";
//}
//
//- (void)request:(WBHttpRequest *)request didFailWithError:(NSError *)error;
//{
//    UIAlertView *alert = nil;
//    
//    alert = [[UIAlertView alloc] initWithTitle:@"请求异常"
//                                       message:[NSString stringWithFormat:@"%@",error]
//                                      delegate:nil
//                             cancelButtonTitle:@"确定"
//                             otherButtonTitles:nil];
//    [alert show];
//    [alert release];
//}
- (void)setButtonClick:(UIButton *)button
{
    
    SettingViewController *set = [[SettingViewController alloc] init];
    [set setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentViewController:set animated:YES completion:^{
        
    }];
    [set release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[MineTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    [cell setBackgroundColor:[UIColor clearColor]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        [cell.aImage setImage:[UIImage imageNamed:@"我的收藏.png"]];
        [cell.lable setText:@"我的收藏"];
        cell.lable.font = [UIFont boldSystemFontOfSize:18];
        cell.lable.textColor = [UIColor whiteColor];
        [cell.bImage setImage:[UIImage imageNamed:@"向右箭头.png"]];
    } else if (indexPath.row == 1)
    {
     
        [cell.aImage setImage:[UIImage imageNamed:@"我的历史.png"]];
        [cell.lable setText:@"我的历史"];
        cell.lable.font = [UIFont boldSystemFontOfSize:18];
        cell.lable.textColor = [UIColor whiteColor];
        [cell.bImage setImage:[UIImage imageNamed:@"向右箭头.png"]];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 0) {
        
        LikeViewController *like = [[LikeViewController alloc] init];
        [like.array addObjectsFromArray:[CellectDatabase selectAll]];
        [like.likeTableView reloadData];
        [like setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentViewController:like animated:YES completion:nil];
        [like release];
    } else if (indexPath.row == 1)
    {
        
        HistoryViewController *history = [[HistoryViewController alloc] init];
        [history.array addObjectsFromArray:[HistoryDatabase selectAll]];
        [history.HistoryTableView reloadData];
        [history setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentViewController:history animated:YES completion:nil];
        [history release];
        
    }
}







@end
